package com.woniuxy.microgoodsserver.sortmag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo1;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo2;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcommonserver.pageHelper.TableResult;
import com.woniuxy.microcommonserver.res.ResultMsg;
import com.woniuxy.microgoodsserver.sortmag.service.GoodCategoryService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/*后台系统分类的接口
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/22 9:54
 */
@RequestMapping("sort")
@RestController
public class GoodCategoryController {
    @Resource
    private GoodCategoryService goodCategoryServiceImpl;
    /**
        增添分类目录
     * @params:
     * @return
     */
    @PostMapping(value = "/addSort",produces = MediaType.APPLICATION_JSON_VALUE)
    public String addSort(GoodCategoryInfo1 goodCategoryInfo1){
        //先判断分类名称是否存在
        GoodCategoryInfo1 goodCategoryInfo11 = goodCategoryServiceImpl.findByName(goodCategoryInfo1);
        if(goodCategoryInfo11!=null){
            //说明存在，返回不能创建当前目录
            return JSONObject.toJSONString(ResultMsg.builder().code(201).msg("该目录已存在,请重新创建").build());
        }else {
            goodCategoryServiceImpl.addSort(goodCategoryInfo1);
            return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("添加成功").build());
        }
    }
    /**
        查询所有的一级分类
     * @params:
     * @return
     */
    @GetMapping(value = "/findSort",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult findSort(PageRequest pageRequest){
        PageInfo<GoodCategoryInfo1> allParent = goodCategoryServiceImpl.findAllParent(pageRequest.getPageNum(),pageRequest.getLimit());
        long total = allParent.getTotal();
        List<GoodCategoryInfo1> allParentList = allParent.getList();
        TableResult<GoodCategoryInfo1> tr = new TableResult<GoodCategoryInfo1>();
        tr.setTotal(total);
        tr.setRows(allParentList);
        return tr;
    }
    /**
       根据一级类目查询二级子目
     * @params:
     * @return
     */
    @GetMapping(value = "/findSortById",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult findSortById(PageRequest pageRequest,GoodCategoryInfo1 goodCategoryInfo1){
        PageInfo<GoodCategoryInfo2> sortById =
                goodCategoryServiceImpl.findSortById(pageRequest, goodCategoryInfo1);
        List<GoodCategoryInfo2> list = sortById.getList();
        long total = sortById.getTotal();
        TableResult<GoodCategoryInfo2> tr = new TableResult<>();
        tr.setTotal(total);
        tr.setRows(list);
        return tr;
    }
    /**
        根据ID删除分类
     * @params:
     * @return
     */
    @DeleteMapping(value = "/deleteSortById",produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteSortById(GoodCategoryInfo1 goodCategoryInfo1){

        goodCategoryServiceImpl.deleteSortById(goodCategoryInfo1);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("删除成功").build());
    }


    /**根据ID查找目录分类，展示在修改页面上

     * @params:
     * @return
     */
    @GetMapping(value = "/getSort",produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSort(GoodCategoryInfo1 goodCategoryInfo1){
        List<Map> goodCategoryInfo11 = goodCategoryServiceImpl.getSort(goodCategoryInfo1);
        return JSONObject.toJSONString(goodCategoryInfo11);
    }

    /**修改分类信息

     * @params:
     * @return
     */
    @PutMapping(value = "/updateSort",produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateSort(GoodCategoryInfo1 goodCategoryInfo1){
        goodCategoryServiceImpl.updateSort(goodCategoryInfo1);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("修改成功").build());
    }
}
